package com.situ.car.dao.Impl;

import com.situ.car.dao.IPermissionDao;
import com.situ.car.pojo.entity.Permission;
import com.situ.car.pojo.entity.Role;
import com.situ.car.pojo.vo.RoleVo;
import com.situ.car.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class PermissionDaoImpl implements IPermissionDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Permission> selectAll(Integer roleId) {
        return template.query("SELECT p.id,p.permission_name,p.path\n" +
                "FROM role_permission AS rp\n" +
                "JOIN permission AS p ON rp.permission_id = p.id\n" +
                "WHERE rp.role_id = ?", new BeanPropertyRowMapper<>(Permission.class), roleId);
    }

    @Override
    public List<RoleVo> selectPath(Integer roleId) {
        return template.query("SELECT rp.role_id,p.id,p.permission_name,p.path\n" +
                "FROM role_permission AS rp\n" +
                "JOIN permission AS p ON rp.permission_id = p.id\n" +
                "WHERE rp.role_id = ?", new BeanPropertyRowMapper<>(RoleVo.class), roleId);
    }
}
